import axios from "axios";
import {userInfoStore} from "../store";
import router from "../router";

const http = axios.create({
    baseURL: "http://localhost:8080",
    timeout: 30000
})

const userInfo = userInfoStore()


http.interceptors.request.use(config => {
    config.headers.Authorization = "Bearer "+userInfo.token;
    return config
}, err => {
    console.log(err)
    return Promise.reject(err)
})

http.interceptors.response.use(config => {
    //直接返回后端数据
    const data: any = config.data;
    if (data.code == 401) {
        console.log(120)
        router.clearRoutes()
    }
    return data;
}, err => {
    console.log(err)
    return Promise.reject(err)
})

export default http
